using System;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class ObserveList<TValue> : List<TValue>
{
    private Action _onValueChange;

    public void TrySetAction(Action onValueChange)
    {
        _onValueChange = onValueChange;
    }

    public ObserveList()
    {

    }

    public new void Add(TValue item)
    {
        base.Add(item);
        _onValueChange?.Invoke();
    }

    public new void AddRange(IEnumerable<TValue> collection)
    {
        base.AddRange(collection);
        _onValueChange?.Invoke();
    }

    public new void Clear()
    {
        base.Clear();
        _onValueChange?.Invoke();
    }

    public new List<TOutput> ConvertAll<TOutput>(Converter<TValue, TOutput> converter)
    {
        return base.ConvertAll(converter);
    }

    public new void Insert(int index, TValue item)
    {
        base.Insert(index, item);
        _onValueChange?.Invoke();
    }

    public new void InsertRange(int index, IEnumerable<TValue> collection)
    {
        base.InsertRange(index, collection);
        _onValueChange?.Invoke();
    }

    public new bool Remove(TValue item)
    {
        bool suc = base.Remove(item);
        if(suc)
            _onValueChange?.Invoke();
        return suc;
    }

    public new int RemoveAll(Predicate<TValue> match)
    {
        int result = base.RemoveAll(match);
        _onValueChange?.Invoke();
        return result;
    }

    public new void RemoveAt(int index)
    {
        base.RemoveAt(index);
        _onValueChange?.Invoke();
    }

    public new void RemoveRange(int index, int count)
    {
        base.RemoveRange(index, count);
        _onValueChange?.Invoke();
    }

    public new void Reverse()
    {
        base.Reverse();
        _onValueChange?.Invoke();
    }

    public new void Sort(Comparison<TValue> comparison)
    {
        base.Sort(comparison);
        _onValueChange?.Invoke();
    }

    public new TValue this[int index]
    {
        get
        {
            return base[index];
        }
        set
        {
            Debug.Assert(value != null);
            if (!value.Equals(base[index]))
            {
                base[index] = value;
                _onValueChange?.Invoke();
            }
        }
    }
}

